package com.tina.tinasso.controller;

import com.tina.tinasso.service.JwtService;
import com.tina.tinasso.util.RedisUtil;
import com.tina.tinasso.util.ResultFormat;
import com.tina.tinasso.util.ResultUtil;
import com.tina.tinasso.util.ShortMessageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @author Tina
 * @version 0.0.1
 * @date 2020/7/2 16:30
 **/
@RestController
public class HomeController {

    @Autowired
    private static RedisUtil redisUtil;

    @Autowired
    private JwtService service;

    /**
     * 获取认证码
     *
     * @param phone
     * @return
     */
    public ResultFormat captchaUtil(String phone) {
        if (!ShortMessageUtil.isMobileNO(phone)) {
            return ResultUtil.error(100, "手机号输入错误");
        }
        Integer captcha = ShortMessageUtil.SendMsg(phone);
        if (captcha != null) {
            redisUtil.set(phone + ":" + captcha, captcha, 300);
            return ResultUtil.success(captcha);
        }
        return ResultUtil.error();
    }


    /**
     * 获取认证码
     *
     * @param phone
     * @return
     */
    @GetMapping("/captcha")
    public ResultFormat captcha(String phone) {
        return captchaUtil(phone);
    }

    /**
     * 登录
     *
     * @param phone
     * @param captcha
     * @return
     */
    @PostMapping("/login")
    public ResultFormat login(String phone, Integer captcha) {
        return service.login(phone, captcha);
    }


}
